package cn.it.facultystaff.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.json.JSONArray;
import org.json.JSONObject;

import cn.it.facultystaff.entity.FacultyStaff;
import cn.it.facultystaff.service.FacultyStaffService;
import cn.it.facultystaff.service.UploadFileServer;
import cn.it.facultystaff.service.impl.FacultyStaffServiceImpl;
import cn.it.facultystaff.service.impl.UploadFileServerImpl;

/**
 * Servlet implementation class FacultyStaffServlet
 */
public class FacultyStaffServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private FacultyStaffService facultyStaffService=new FacultyStaffServiceImpl();
	private UploadFileServer uploadFileServer=new UploadFileServerImpl();
	
    /**
     * Default constructor. 
     */
    public FacultyStaffServlet() {
        // TODO Auto-generated constructor stub
    }
    

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
		
		//String path=getServletContext().getRealPath("./")+File.separator+"photo"+File.separator;
		String path="D:/FacultyStaff/photo/";
		System.out.println(path);
		
		response.setCharacterEncoding("utf-8");        
	    response.setContentType("text/html; charset=utf-8");
		
		String action=request.getParameter("action");
		
		if("find".equals(action)){
			String jobNumber=request.getParameter("jobNumber");
			FacultyStaff facultyStaff=facultyStaffService.getFacultyStaffByIdNumber(jobNumber);
			//request.setAttribute("data", facultyStaff);
			JSONObject json=new JSONObject();
			
			if(facultyStaff.getJobNumber()!=null){
				if(!facultyStaff.getJobNumber().equals("")){
					json.put("jobNumber", facultyStaff.getJobNumber());
					
					String photopath=facultyStaff.getPhotopath().substring(facultyStaff.getPhotopath().lastIndexOf("/")+1);
					
					json.put("photoPath", photopath);
					
					PrintWriter out=response.getWriter();
					
					out.println(json);
					out.flush();
					out.close();
				}
				
			}
			
			
		}else if("update".equals(action)){
			Map<String,String> params=uploadFileServer.uploadFile(request,path);
			facultyStaffService.updateFacultyStaffInfo(params);
			
			PrintWriter out=response.getWriter();
			out.print("<script language='javascript'>alert('修改信息成功');window.location.href='/views/updateInfo.jsp';</script>");
			out.flush();
			out.close();
			//response.sendRedirect("/FacultyStaff/views/updateInfo.jsp");
			
		}else if("add".equals(action)){
			Map<String,String> params=uploadFileServer.uploadFile(request,path);
			
			facultyStaffService.addFacultyStaffInfo(params);
			
			//response.sendRedirect("/FacultyStaff/views/addInfo.jsp");
			PrintWriter out=response.getWriter();
			out.print("<script language='javascript'>alert('添加信息成功');window.location.href='/views/addInfo.jsp';</script>");
			out.flush();
			out.close();
		}else if("teacher".equals(action)){
			
			List<String> teacherPartment=facultyStaffService.getTeacherDepartment();
			JSONObject json=new JSONObject();
			JSONArray jsonArray=new JSONArray();
			
			for(String department:teacherPartment){
				JSONObject jsonObject=new JSONObject();
				jsonObject.put("department", department);
				jsonArray.put(jsonObject);
			}
			json.put("teacherPartment", jsonArray);
			
			PrintWriter out=response.getWriter();
			
			out.println(json);
			out.flush();
			out.close();
		}else if("student".equals(action)){
			List<String> studentPartment=facultyStaffService.getStudentDepartment();
			
			JSONObject json=new JSONObject();
			JSONArray jsonArray=new JSONArray();
			
			for(String department:studentPartment){
				JSONObject jsonObject=new JSONObject();
				jsonObject.put("department", department);
				jsonArray.put(jsonObject);
			}
			json.put("teacherPartment", jsonArray);
			
			PrintWriter out=response.getWriter();
			
			out.println(json);
			out.flush();
			out.close();
		}
		
		
		
	}

}
